package ve.com.wayoyo.wuml_case;

import java.io.File;
import java.io.FilenameFilter;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class FileSelector extends Dialog {

	private ArrayAdapter<String> adapter;
	private String[] mFileList;
	private String root = Environment.getExternalStorageDirectory() + "WUML//UNTITLED_PROYECT//CODIGO//";
	private String[] listPath;
	private File mPath = new File(root);
	private int pos = 1;
	private String mChosenFile;

	public FileSelector(Context context,ArrayAdapter<String> adapter) {
		super(context);
		this.adapter = adapter;
	}

	public void alert(String mensaje)
	{
		Toast.makeText(this.getContext(), mensaje , Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {	
		super.onCreate(savedInstanceState);
		setContentView(R.layout.file_selector);
		setTitle("Seleccione el archivo");		
		listPath = root.split("//");
		int pageHeight = UserPref.getInstancia().getWinHeight();			
		if (pageHeight<=480){
			LayoutParams params = getWindow().getAttributes();	
			params.width=LayoutParams.MATCH_PARENT;
			params.height= 200;
			ListView lista = (ListView)findViewById(R.id.listFiles);
			lista.setLayoutParams(params);
		}
		Button btBack = (Button)findViewById(R.id.btback);
		btBack.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				pos= pos-1;
				FileSelector.this.act_root(pos);
			}
		});


		CheckBox nuevoArchivo = (CheckBox)findViewById(R.id.existNewFile);
		final EditText etNewFile = (EditText)findViewById(R.id.NewFile);
		nuevoArchivo.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					etNewFile.setEnabled(true);
				}
				else {
					etNewFile.setEnabled(false);
				}
			}
		});

		ListView listView = (ListView) findViewById(R.id.listFiles);	
		loadFileList();
		for (String st : mFileList) {
			this.adapter.add(st);
		}		
		listView.setAdapter(adapter);

		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				FileSelector.this.selectFile(position);
			}
		});

	}

	protected void selectFile(int position) {
		String selected = (mFileList[position]);		
		root = root +"//" +selected;
		listPath = root.split("//");
		mPath = new File(root);		
		if (!mPath.isDirectory()){
			mChosenFile = root;
			alert("archivo seleccionado "+ selected);
		}else{		
			mPath = new File(root);
			pos = pos+1;
			listPath[pos]=selected;
			act_root(pos);
		}
	}

	protected void act_root(int pos) {
		root = "";
		for (int i=0;i<=pos;i++){
			if (i!=0)
				root = root + "//"+listPath[i];
			else
				root = listPath[i];
		}			
		mPath = new File(root);	
		actList();
	}

	private void actList(){
		for (String st : mFileList) {
			this.adapter.remove(st);
		}
		
		loadFileList();		
		for (String st : mFileList) {
			this.adapter.add(st);
		}
	}

	private void loadFileList() {
		try {
			mPath.mkdirs();
		}
		catch(SecurityException e) {
			//	        Log.e(TAG, "unable to write on the sd card " + e.toString());
		}
		if(mPath.exists()) {
			FilenameFilter filter = new FilenameFilter() {
				public boolean accept(File dir, String filename) {
					File sel = new File(dir, filename);
					return true;
				}
			};
			mFileList = mPath.list(filter);
		}
		else {
			
			mFileList= new String[0];
		}
	}

}
